package com.whacos.modules.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whacos.common.utils.FileType;
import com.whacos.common.utils.IdUtils;
import com.whacos.common.utils.MD5Utils;
import com.whacos.common.utils.ObjectUtils;
import com.whacos.common.utils.StringUtils;
import com.whacos.common.utils.thirdparty.OssUtils;
import com.whacos.modules.system.dao.FileDao;
import com.whacos.modules.system.dto.FileDTO;
import com.whacos.modules.system.entity.File;
import com.whacos.modules.system.service.FileService;
import com.whacos.modules.system.vo.FileVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author xiongdun
 * @description SysFileServiceImpl
 * @since 2018/11/17 17:11
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileDao, File> implements FileService {

    @Autowired
    private FileDao fileDao;

    @Override
    public Boolean isExist(String url) {
        Boolean isExist = false;
        if (!StringUtils.isEmpty(url)) {
            List<File> fileList = fileDao.selectList(Wrappers.lambdaQuery(new File()).eq(File::getUrlAddress, url));
            if (ObjectUtils.isNotEmpty(fileList)) {
                isExist = true;
            }
        }
        return isExist;
    }

    @Override
    public FileVO uploadFile(Long userId, MultipartFile file) throws IOException {
        String oldFileName = file.getOriginalFilename();
        String contentType = file.getContentType();
        Long fileSize = file.getSize();
        Long recordId = IdUtils.getId();

        String mimeType = "";
        if (StringUtils.isNotEmpty(oldFileName) && oldFileName.contains(".")) {
            int index = StringUtils.getLastMatchingIndex(oldFileName, ".");
            mimeType = oldFileName.substring(index, oldFileName.length());
        }
        String newFileName = MD5Utils.encrypt(recordId + "") + mimeType;
        String fileUrl = OssUtils.uploadFile(newFileName, file.getInputStream());
        File sysFile = new File();
        sysFile.setUrlAddress(fileUrl); // 文件地址
        sysFile.setSize(fileSize); // 文件大小
        sysFile.setKeyId(MD5Utils.encrypt(fileUrl)); // url key 唯一
        sysFile.setFileName(newFileName); // 文件名
        sysFile.setSuffix(contentType);
        sysFile.setType(FileType.fileType(oldFileName));
        sysFile.setId(recordId);
        sysFile.createdData(userId);
        if (this.fileDao.insert(sysFile) > 0) {
            FileVO fileVO = new FileVO();
            fileVO.setResult(1);
            fileVO.setUrl(fileUrl);
            fileVO.setId(recordId);
            return fileVO;
        }
        return null;
    }

    @Override
    public int deleteFile(String fileName) {
        OssUtils.deleteFile(fileName);
        return 1;
    }

    @Override
    public FileVO get(FileDTO fileDTO) {
        return null;
    }
}
